<?php
namespace Admin\Controller;

use Common\Base\ControllerBase;
use Common\Logic\Common\ConfigLogic;

class ConfigController extends ControllerBase
{
    public $L_config;

    public function _initialize()
    {
        $this->L_config = new ConfigLogic();
    }

    public function index()
    {
        if(IS_POST){
            $params = I('post.');
            if (false === $this->L_config->updateConfig($params,$_FILES)) {
                $this->error($this->L_config->getError());
                exit;
            }
            /* 读取站点配置 */
            $config = $this->L_config->getGlobalConfig();
            C($config);//重新添加载入配置
            $this->success('更新成功', U('config/index'));
            exit;
        }
        $this->assign('cate', $this->L_config->getAllCateConfig());
        $this->display();
    }

    /**
     * 添加全局配置
     *
     * @return void
     */
    public function add()
    {
        if (IS_POST){
            $params = I('post.');
            if(false === $this->L_config->editConfig($params)){
                $this->error($this->L_config->getError());
                exit;
            }
            $this->success('添加成功', U('config/index'));
            exit;

        }else{
            $this->assign('searchOptions', $this->L_config->getSearchOption());
        }

        $this->assign('action', 'add');
        $this->display('edit');
    }

    /**
     * 编辑全局配置
     *
     * @return void
     */
    public function edit()
    {
        if(IS_POST){
            $params = I('post.');
            if(false === $this->L_config->editConfig($params, $params['id'])){
                $this->error($this->L_config->getError());
                exit;
            }
            $this->success('编辑成功', U('config/index'));
            exit;
        }else{
            $config_id = I("get.config_id", 0);
            $config = $this->L_config->getConfigById($config_id);
            $config['type_options'] = unserialize($config['type_options']);
            $this->assign('config', $config);
            $this->assign('searchOptions', $this->L_config->getSearchOption());
        }
        $this->assign('action', 'edit');
        $this->display();
    }

    /**
     * 删除配置
     *
     * @return void
     */
    public function del()
    {
        $config_id = (int)I('get.config_id');
        if($config_id < 1){ $this->error('参数错误'); }
        if(false === $this->L_config->deleteConfigById($config_id)){
            $this->error($this->L_config->getError());
        }
        $this->success('删除成功', U('config/index'));
    }
}